package com.company.demo.control;


import com.company.demo.beans.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller//依赖注入
public class ThymeleafController {

    @RequestMapping("/test/myindex")
   public  String  indexHandle(Model model){

       //th：text
       model.addAttribute("welcome","hello thymelaf world");
       //th:utext
       model.addAttribute("welcome1","<h2>hello <br>thymelaf world</h2>");

       //添加student类
        //定义student类的参数，进行页面的连接调用
        Student student = new Student("张三",23);
        model.addAttribute("student",student);

        //获取标签动态name属性值，默认value值
        model.addAttribute("attrName","age");


        //th:if用法
        model.addAttribute("gender","male");

        //th:swith,th:case
        model.addAttribute("age",23);

        //属性用于遍历th:each,th:text
        Student student1 = new Student("张三",35);
        Student student2 = new Student("李四",52);
        Student student3 = new Student("王五",85);

        //创建list集合
        List<Student> studentList = new ArrayList<>();
        studentList.add(student1);
        studentList.add(student2);
        studentList.add(student3);
        model.addAttribute("studentList",studentList);


        //遍历map
        Map<String,Student> stuMap = new HashMap<>();
        stuMap.put("stu1",student1);
        stuMap.put("stu2",student2);
        stuMap.put("stu3",student3);
        model.addAttribute("stuMap",stuMap);


        //添加图片
        model.addAttribute("photo","cat.jpg");

        //内连css
        model.addAttribute("elementId","reddiv");
        model.addAttribute("bgColor","red");

        //布尔字面量
        model.addAttribute("isClose",false);

        //null字面量
     //对象是空时
     Student studentNull = null;
     model.addAttribute("studentNull",studentNull);
     //List的size是0
     List<String> cities = new ArrayList<>();
     model.addAttribute("cities",cities);



     return  "index";//返回模板页面
   }

    //内置对象
    @RequestMapping("/test/attr")
    public  String attrHandle(HttpServletRequest request, HttpSession session, Model model){
        request.setAttribute("req","reqValue");
        session.setAttribute("ses","sesValue");
        session.getServletContext().setAttribute("app","appValue");

        int[] nums = {1,2,3,4,5};
        model.addAttribute("nums",nums);
        model.addAttribute("today",new Date());
        model.addAttribute("cardId","3213654987");

        return "index2";
    }

    
}
